package toy.keli.edic.data.parse

import android.os.CountDownTimer
import com.google.mlkit.vision.common.InputImage
import com.google.mlkit.vision.text.TextRecognition
import com.google.mlkit.vision.text.TextRecognizer
import com.google.mlkit.vision.text.latin.TextRecognizerOptions
import okhttp3.internal.wait
import toy.keli.edic.domain.model.UtilItem
import toy.keli.edic.util.utilImage
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit

class Image2Text {
    companion object {
        fun convert(image: InputImage,onSucces:(List<String>)->Unit): List<String> {
            val options = TextRecognizerOptions.DEFAULT_OPTIONS
            val recognizer:TextRecognizer = TextRecognition.getClient(options)
            var rt=listOf<String>()
            recognizer.process(image).addOnSuccessListener{ text: com.google.mlkit.vision.text.Text? ->
                if (text != null) {
                    rt= text.textBlocks.map { it.text }
                    onSucces(rt)
                }
            }.addOnFailureListener{e:Exception->
                e.printStackTrace()
            }

            return rt
        }
    }
}

private fun InputImage.thumbnail(w: Int): InputImage {
    var source = utilImage.thumbnail(this.bitmapInternal,w)
    return InputImage.fromBitmap(source,0)
}
